#!/usr/bin/env python
# encoding: utf-8


"""
@file: triangle_cond_action_funcs.py
@time: 2017/2/16 下午2:45
"""
from mathsolver.functions.base import *
from sympy import sin, cos, symbols, simplify, expand_trig, pi, tan, FiniteSet, cot, collect_const
from mathsolver.functions.budengshi import common_opers as co
from itertools import permutations, chain
from mathsolver.functions.sanjiao.triangle_condition_converts import _convert1
from mathsolver.functions.sanjiao import sanjiaoxing_utils as sjx_u
from sympy.core.numbers import ComplexInfinity
from mathsolver.functions.sanjiao.triangle_structs import ActionFunc

# 条件作用函数
# 一个条件，
trig_subs = (('sin(A)', 'SINA'), ('cos(A)', 'COSA'), ('tan(A)', 'TANA'), ('cot(A)', 'COTA'),
             ('sin(B)', 'SINB'), ('cos(B)', 'COSB'), ('tan(B)', 'TANB'), ('cot(B)', 'COTB'),
             ('sin(C)', 'SINC'), ('cos(C)', 'COSC'), ('tan(C)', 'TANC'), ('cot(C)', 'COTC'))

trig_reverse_subs = map(lambda pair: (pair[1], pair[0]), trig_subs)

ANGLES = FiniteSet(*symbols('A, B, C'))


# 一个条件
# 最简单的只有一个符号变量，可以计算出值得
def _one_cond_action_1(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 1:
        raise Exception('Type Match Error')
    cond_eq = cond_eq_list[0]
    cond_eq_f = (cond_eq[0] - cond_eq[1]).subs(trig_subs)
    symbs = list(cond_eq_f.free_symbols)
    if not len(symbs) == 1:
        raise Exception('Type Match Error')
    symb = symbs[0]
    solve_results = solve(cond_eq_f)
    for result in solve_results:
        action_results[symb.subs(trig_reverse_subs)] = result
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '求得:' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '求得:' + BaseEqs(_eqs).printing()])
    return action_results, af


# 余弦定理理解三角形
def _one_cond_action_2(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 1:
        raise Exception('Type Match Error')
    cond_eq = cond_eq_list[0]
    cond_eq_f = (cond_eq[0] - cond_eq[1])
    cos_tuples = (('cos(A)', 'b**2 + c**2 - a**2', '2*b*c'), ('cos(B)', 'a**2 + c**2 - b**2', '2*a*c'),
                  ('cos(C)', 'a**2 + b**2 - c**2', '2*a*b'))
    af.add_label('余弦定理')
    cond_eq_f = collect_const(cond_eq_f)
    for cos_angle, mo, de in cos_tuples:
        s_v = solve(cond_eq_f, mo)
        if s_v:
            cos_v = s_v[0] / sympify(de)
            if cos_v.is_real:
                action_results[sympify(cos_angle)] = cos_v
                angle = list(sympify(cos_angle).free_symbols)[0]
                action_results[sin(angle)] = sqrt(1 - cos_v ** 2)
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '由余弦定理得:' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '由余弦定理得:' + BaseEqs(_eqs).printing()])
    return action_results, af


# 正弦理解三角形
# △ABC在内角A、B、C的对边分别为a,b,c,已知[a=bcosC+csinB].
def _one_cond_action_3(cond_eqs):
    """
    :param cond_eqs:
    :return:
    """
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 1:
        raise Exception('Type Match Error')
    cond_eq = cond_eq_list[0]
    cond_eq_f = (cond_eq[0] - cond_eq[1])
    f_list = []
    angles_subs = (('A', 'pi-(B+C)'), ('B', 'pi - (A+C)'), ('C', 'pi-(A+B)'))
    for angles_sub in angles_subs:
        f1 = expand_trig(sin(angles_sub[0]) - sin(angles_sub[1]))
        f2 = expand_trig(cos(angles_sub[0]) - cos(angles_sub[1]))
        f_list.append(f1)
        f_list.append(f2)
    for f in f_list:
        tmp_f = cond_eq_f - f
        items = tmp_f.as_coefficients_dict().items()
        if len(items) != 2 and len(tmp_f.free_symbols) != 2:
            continue
        mon1, mon2 = map(lambda _: _[0] * _[1], items)
        mon_gcd = co.i_gcd(mon1, mon2)
        tmp_f = expand_trig(simplify(tmp_f / mon_gcd))
        if len(tmp_f.free_symbols) != 1:
            continue
        trig_values = _convert1(BaseEq([tmp_f, 0]))
        action_results.update(dict(trig_values))
    af.add_label('正弦定理')
    af.add_label('三角形内角和')
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '由三角形内角和得:' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), '由三角形内角和得:' + BaseEqs(_eqs).printing()])
    return action_results, af


# 592752,"{""main"": ""设△ABC的内角A,B,C所对的边长分别为a,b,c,acosB-bcosA=\\frac{3}{5}c.求\\frac{tanA}{tanB}的值
def _con_cond_action_4(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 1:
        raise Exception('Type Match Error')
    cond_eq = cond_eq_list[0]
    cond_eq_f = (cond_eq[0] - cond_eq[1])
    angle_tan_fracs = [tan(a1) / tan(a2) for a1, a2 in permutations(['A', 'B', 'C'], 2)]
    tan_subs = (('tan(A)', 'sin(A)/cos(A)'), ('tan(B)', 'sin(B)/cos(B)'), ('tan(C)', 'sin(C)/cos(C)'))
    for angle_tan_frac in angle_tan_fracs:
        tmp_f = angle_tan_frac.subs(tan_subs)
        mo, de = co.complete_fraction(tmp_f)
        r1 = solve(cond_eq_f, mo)
        if r1 and len(r1) == 1:
            sub_mo = r1[0]
            tmp_f_value = tmp_f.subs(mo, sub_mo)
            if tmp_f_value.is_real:
                action_results[angle_tan_frac] = tmp_f_value
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['\\because ' + BaseEq(cond_eqs[0]).printing(), '\\therefore ' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['\\because ' + BaseEq(cond_eqs[0]).printing(), '\\therefore ' + BaseEqs(_eqs).printing()])
    return action_results, af


# 已知 两个边的关系[或者角的关系] 求两边的一个代数关系 比如知道 a = 2*b 求 a/b
def _one_cond_action_5(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 1:
        raise Exception('Type Match Error')
    cond_eq = cond_eq_list[0]
    cond_eq_f = (cond_eq[0] - cond_eq[1])
    symb_f, const = co.split_mons_const(cond_eq_f)
    action_results[sympify(str(symb_f))] = -const
    s1, s2 = list(cond_eq_f.free_symbols)
    s1_v = solve(cond_eq_f, s1)[0]
    action_results[s1 / s2] = s1_v / s2
    action_results[s2 / s1] = s2 / s1_v
    a1 = str(s1).upper()
    sin_a1 = sin(a1)
    a2 = str(s2).upper()
    sin_a2 = sin(a2)
    action_results[sin_a1 / sin_a2] = s1_v / s2
    action_results[sin_a2 / sin_a1] = s2 / s1_v
    _eqs = action_results.items()
    af.add_step(['由 ' + BaseEq(cond_eq_list[0]).printing(), '得出' + BaseEqs(_eqs).printing()])
    return action_results, af


# 只包含边角关系的 可以假设某一条边的值
def _one_cond_action_6(cond_eqs):
    action_results = {}
    af = ActionFunc()
    cond_eq_list = cond_eqs.sympify()
    cond_f_list = map(lambda pair: pair[0] - pair[1], cond_eq_list)
    _a, _b, _c = symbols('a,b,c')
    flag = True
    _eqs = []
    for _f in cond_f_list:
        symbs = list(_f.free_symbols)
        if len(symbs) == 1 and symbs[0] in [_a, _b, _c]:
            flag = False
            break
        if len(cond_eq_list) == 1:
            af.add_step(['\\because ' + BaseEq(cond_eq_list[0]).printing(), ''])
        else:
            af.add_step(['\\because ' + cond_eqs.printing(), ''])
        _f = expand_trig(_f)
        _f2 = _f.subs(sin('A'), 'a/(2*r)').subs(cos('A'), '(b**2 + c**2 - a**2)/(2*b*c)')
        if _f2 != _f:
            af.add_step(['\\because ' + BaseEq([_f, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f2]).printing()])
            _eqs.append(['((b**2 + c**2 - a**2)/(2*b*c))**2 + (a/(2*r))**2', '1'])
        _f3 = _f2.subs(sin('B'), 'b/(2*r)').subs(cos('B'), '(a**2 + c**2 - b**2)/(2*a*c)')
        if _f3 != _f2:
            af.add_step(['\\because ' + BaseEq([_f2, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f3]).printing()])
            _eqs.append(['((a**2 + c**2 - b**2)/(2*a*c))**2 + (b/(2*r))**2', '1'])
        _f4 = _f3.subs(sin('C'), 'c/(2*r)').subs(cos('C'), '(a**2 + b**2 - c**2)/(2*a*b)')
        if _f4 != _f3:
            af.add_step(['\\because ' + BaseEq([_f3, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f4]).printing()])
            _eqs.append(['((a**2 + b**2 - c**2)/(2*a*b))**2 + (c/(2*r))**2', '1'])
        _eqs.append([_f4, '0'])
    af.add_label('正弦定理')
    af.add_label('余弦定理')
    if flag:  # 如果可以，那么设其中一条边的值为1
        assuem_eq = ['b', '1']
        # _m = sympify('m')
        # af.add_step(['假设令' + BaseEq([_b, _m]).printing(), ':'])
        extend_eqs = list(chain(_eqs, [assuem_eq]))
        # _extend_eqs = list(chain(_eqs, [[_b, _m]]))
        print_eqs = BaseEqs(_eqs)
        af.add_step(['得到方程组:', print_eqs.printing()])
        abc_list = co.isolve_eqs(BaseEqs(extend_eqs), [_a, _b, _c])
        # 合法的边
        abc_list = filter(
            lambda _: sjx_u.legal_sided(_[0][1]) and sjx_u.legal_sided(_[1][1]) and sjx_u.legal_sided(
                _[2][1]), abc_list)
        # 是否满足两边之和大于第三边
        abc_list = list(filter(lambda _: sjx_u.legal_sides((_[0][1], _[1][1], _[2][1])), abc_list))
        a_v, b_v, c_v = map(simplify, zip(*abc_list[0])[1])
        print_eqs = BaseEqs([['a', a_v * _b], ['c', c_v * _b]])
        af.add_step(['验证求得:', print_eqs.printing()])
        cos_a = simplify((b_v ** 2 + c_v ** 2 - a_v ** 2) / (2 * b_v * c_v))
        action_results[cos('A')] = cos_a
        sin_a = sqrt(1 - cos_a ** 2)
        action_results[sin('A')] = sin_a
        tan_a = sin_a / cos_a
        if not isinstance(tan_a, ComplexInfinity):
            action_results[tan('A')] = tan_a
        cot_a = cos_a / sin_a
        if not isinstance(cot_a, ComplexInfinity):
            action_results[cot('A')] = cot_a
        cos_b = simplify((a_v ** 2 + c_v ** 2 - b_v ** 2) / (2 * a_v * c_v))
        action_results[cos('B')] = cos_b
        sin_b = sqrt(1 - cos_b ** 2)
        action_results[sin('B')] = sin_b
        tan_b = sin_b / cos_b
        if not isinstance(tan_b, ComplexInfinity):
            action_results[tan('B')] = tan_b
        cot_b = cos_b / sin_b
        if not isinstance(cot_b, ComplexInfinity):
            action_results[cot('B')] = cot_b
        cos_c = simplify((a_v ** 2 + b_v ** 2 - c_v ** 2) / (2 * a_v * b_v))
        action_results[cos('C')] = cos_c
        sin_c = sqrt(1 - cos_c ** 2)
        action_results[sin('C')] = sin_c
        tan_c = sin_c / cos_c
        cot_c = cos_c / sin_c
        if not isinstance(tan_c, ComplexInfinity):
            action_results[tan('C')] = tan_c
        if not isinstance(cot_c, ComplexInfinity):
            action_results[cot('C')] = cot_c
        l1 = permutations([_a, _b, _c], 2)
        rs = map(lambda _: (_[0] / _[1], (_[0] / _[1]).subs(((_a, a_v), (_b, b_v), (_c, c_v)))), l1)
        action_results.update(dict(rs))
        _eqs = action_results.items()
        af.add_step(['求得', BaseEqs(_eqs).printing()])
        return action_results, af
    else:
        raise Exception('Type Match Error')


one_cond_action_funcs = [_one_cond_action_1, _one_cond_action_2, _one_cond_action_3, _con_cond_action_4,
                         _one_cond_action_5, _one_cond_action_6]  # _one_cond_action_6


# 两个条件
def _two_cond_action_1(cond_eqs):
    action_results = {}
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 2:
        raise Exception('Type Match Error')
    solve_eqs = []
    for cond_eq_l, cond_eq_r in cond_eq_list:
        cond_eq_f = cond_eq_l - cond_eq_r
        tmp_f = cond_eq_f.subs(trig_subs)
        solve_eqs.append(tmp_f)
    solve_results = solve(solve_eqs)
    af = ActionFunc()
    for result in solve_results:
        for k, v in result.items():
            if v.is_real and v > 0:
                action_results[k.subs(trig_reverse_subs)] = v
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['由' + cond_eqs.printing(), '得' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['由' + cond_eqs.printing(), '得' + BaseEqs(_eqs).printing()])
    return action_results, af


# 两个等式,三条边的关系，求出余弦值
# 例如 b**2 = a*c; c = 2*a
# 2*(a**2 - c**2) = b**2; 4*b=b**2
def _two_cond_action_2(cond_eqs):
    def _verify_sides(side_values):
        a_symb, b_symb, c_symb = symbols('a, b, c', positive=True)
        for v in side_values.values():
            tmp_v = v.subs((('a', a_symb), ('b', b_symb), ('c', c_symb)))
            if tmp_v < 0:
                return False
        return True

    af = ActionFunc()
    af.labels.append('余弦定理')
    action_results = {}
    cond_eq_list = cond_eqs.sympify()
    if len(cond_eq_list) != 2:
        raise Exception('Type Match Error')
    cond_f_list = map(lambda pair: pair[0] - pair[1], cond_eq_list)
    symbs = sorted(set(co.flatten(map(lambda tmp_f: list(tmp_f.free_symbols), cond_f_list))), key=str)
    if symbs != list(symbols('a, b, c')):
        raise Exception('Type Match Error')
    symb_pairs = (('a', 'b'), ('a', 'c'), ('b', 'c'))
    cos_tuples = (('cos(A)', 'b**2 + c**2 - a**2', '2*b*c'), ('cos(B)', 'a**2 + c**2 - b**2', '2*a*c'),
                  ('cos(C)', 'a**2 + b**2 - c**2', '2*a*b'))
    for symb_pair in symb_pairs:
        solve_results = solve(cond_f_list, *symb_pair)
        if solve_results:
            for solve_result in solve_results:
                if not _verify_sides(solve_result):
                    continue
                for cos_angle, mo, de in cos_tuples:
                    cos_v = (sympify(mo) / sympify(de)).subs(solve_result.items())
                    if cos_v.is_real:
                        action_results[sympify(cos_angle)] = cos_v
    _eqs = list(action_results.items())
    if len(_eqs) == 1:
        af.add_step(['\\because ' + cond_eqs.printing(), '由余弦定理得:' + BaseEq(_eqs[0]).printing()])
    else:
        af.add_step(['\\because ' + cond_eqs.printing(), '由余弦定理得:' + BaseEqs(_eqs).printing()])
    return action_results, af


# 使用诱导公式
# 三角形的内角分别为A,B,C.如果知道其中的2个角的三角函数值。那么就可以求出另一个一个三角函数的所有值
# 比如知道 sin(A), sin(B)的值那么可以得出sin(C)的值，cos(C)的值, tan(C)的值, cot(C)的值
def _two_cond_action_3(cond_eqs):
    action_results = {}
    cond_eq_list = cond_eqs.sympify()
    af = ActionFunc()
    if len(cond_eq_list) != 2:
        raise Exception('Type Match Error')
    cond_f_list = map(lambda pair: pair[0] - pair[1], cond_eq_list)
    cond_symbols = sorted(set(co.flatten(map(lambda tmp_f: list(tmp_f.free_symbols), cond_f_list))), key=str)
    cond_angle_set = FiniteSet(*cond_symbols)
    if len(cond_symbols) != 2 or not cond_angle_set.is_subset(ANGLES):
        raise Exception('Type Match Error')
    other_symb = list(ANGLES - cond_angle_set)[0]
    cond_eq1_f, cond_eq2_f = cond_f_list
    cond_eq1_f = cond_eq1_f.subs(trig_subs)
    cond_eq2_f = cond_eq2_f.subs(trig_subs)
    solve_results = solve([cond_eq1_f, cond_eq2_f])
    af.add_label('三角形内角和')
    if isinstance(solve_results, dict):
        subs_result = map(lambda pair: [pair[0].subs(trig_reverse_subs), pair[1]], solve_results.items())
        all_subs = []
        for trig_result in subs_result:
            symb_trig_results = _convert1(BaseEq(trig_result))
            all_subs.extend(symb_trig_results)
        sin_value = expand_trig(sin(other_symb).subs(other_symb, pi - (cond_symbols[0] + cond_symbols[1]))).subs(
            all_subs)
        action_results[sin(other_symb)] = sin_value
        cos_value = expand_trig(cos(other_symb).subs(other_symb, pi - (cond_symbols[0] + cond_symbols[1]))).subs(
            all_subs)
        action_results[cos(other_symb)] = cos_value
        tan_value = expand_trig(tan(other_symb).subs(other_symb, pi - (cond_symbols[0] + cond_symbols[1]))).subs(
            all_subs)
        action_results[tan(other_symb)] = tan_value
        cot_value = expand_trig(cot(other_symb).subs(other_symb, pi - (cond_symbols[0] + cond_symbols[1]))).subs(
            all_subs)
        action_results[cot(other_symb)] = cot_value
        _eqs = list(action_results.items())
        if len(_eqs) == 1:
            af.add_step(['\\because ' + cond_eqs.printing(),
                         '由' + BaseEq(['A+B+C', pi]).printing() + '得出' + BaseEq(_eqs[0]).printing()])
        else:
            af.add_step(['\\because ' + cond_eqs.printing(),
                         '由' + BaseEq(['A+B+C', pi]).printing() + '得出' + BaseEqs(_eqs).printing()])
        return action_results, af
    else:
        pass


# 设△ABC的内角A、B、C所对的边长分别为a、b、c,acosB=3,bsinA=4.求边长a
# 利用了 a/sin(A) = b/sin(B) = c/sin(C) # 使用正弦定理
def _two_cond_action_4(cond_eqs):
    def _stand_triangle_eq(tmp_eq):
        """
        标准化等式, 含有三角关系的在左边， 常量在右边
        :param tmp_eq:
        :return:
        """
        tmp_l, tmp_r = tmp_eq.sympify()
        tmp_f = tmp_l - tmp_r
        trig_mon, const = co.split_mons_const(tmp_f)
        return BaseEq([trig_mon, -const])

    af = ActionFunc()
    af.add_label('正弦定理')
    action_results = {}
    cond_eq_list = cond_eqs.sympify()
    sin_items = ('a/sin(A)', 'b/sin(B)', 'c/sin(C)')
    sin_item_subs = itertools.permutations(sin_items, 2)
    eq1, eq2 = cond_eq_list
    eq1_f = eq1[0] - eq1[1]
    eq2_f = eq2[0] - eq2[1]
    if len(eq1_f.as_coefficients_dict().items()) != 2 or len(eq2_f.as_coefficients_dict().items()) != 2:  # 限定只有2项
        raise Exception('Type Match Error')
    eq1_l, eq1_r = _stand_triangle_eq(BaseEq([eq1_f, '0'])).sympify()
    eq2_l, eq2_r = _stand_triangle_eq(BaseEq([eq2_f, '0'])).sympify()
    ratio_l = eq1_l / eq2_l
    ratio_r = eq1_r / eq2_r
    for tmp_sub in sin_item_subs:
        tmp_ratio_l = ratio_l.subs(*tmp_sub)
        if len(tmp_ratio_l.free_symbols) == 1:
            symb = list(tmp_ratio_l.free_symbols)[0]
            if str(symb) in ['a', 'b', 'c']:  # 暂时保留
                return None
            elif str(symb) in ['A', 'B', 'C']:
                tmp_ratio_eq = co.convert_eq_no_den(BaseEq([tmp_ratio_l.subs(trig_subs), ratio_r]))
                hold_eq = co.convert_eq_no_den(BaseEq([(sin(symb) ** 2 + cos(symb) ** 2).subs(trig_subs), 1]))
                solve_result = co.solve_poly_eqs(BaseEqs([tmp_ratio_eq.sympify(), hold_eq.sympify()]))
                validate_result = []
                for result_pair in solve_result:
                    pair_d = dict(result_pair)
                    trig_key = sin(symb).subs(trig_subs)
                    if pair_d[trig_key] > 0:
                        validate_result.append(result_pair)
                laste_result = validate_result[0]
                eqs2 = []
                result_eqs = map(lambda _: _[0] - _[1], laste_result)
                eqs2.extend(result_eqs)
                eqs2.append(eq1_f.subs(trig_subs))
                eqs2.append(eq2_f.subs(trig_subs))
                result2 = solve(eqs2)[0].items()
                result2 = filter(lambda _: _[1].is_real, result2)
                trig_result = map(lambda pair: [pair[0].subs(trig_reverse_subs), pair[1]], result2)
                action_results.update(dict(trig_result))
                _eqs = list(action_results.items())
                if len(_eqs):
                    af.add_step(['\\because ' + cond_eqs.printing(), '由正弦定理得:' + BaseEq(_eqs[0]).printing()])
                else:
                    af.add_step(['\\because ' + cond_eqs.printing(), '由正弦定理得:' + BaseEqs(_eqs).printing()])
                return action_results, af


# 如果只包含边角关系, 并且没有包含具体边的值，那么可以假设一条边的值 然后求出其他边的值
def _two_cond_action_5(cond_eqs):
    action_results = {}
    cond_eq_list = cond_eqs.sympify()
    af = ActionFunc()
    if len(cond_eq_list) < 2:
        raise Exception('Type Match Error')
    cond_f_list = map(lambda pair: pair[0] - pair[1], cond_eq_list)
    _a, _b, _c = symbols('a,b,c')
    flag = True
    _eqs = []
    for _f in cond_f_list:
        symbs = list(_f.free_symbols)
        if len(symbs) == 1 and symbs[0] in [_a, _b, _c]:
            flag = False
            break
        af.add_step(['\\because ' + cond_eqs.printing(), ''])
        _f = expand_trig(_f)
        _f2 = _f.subs(sin('A'), 'a/(2*r)').subs(cos('A'), '(b**2 + c**2 - a**2)/(2*b*c)')
        if _f2 != _f:
            af.add_step(['\\because ' + BaseEq([_f, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f2, '0']).printing()])
            _eqs.append(['((b**2 + c**2 - a**2)/(2*b*c))**2 + (a/(2*r))**2', '1'])
        _f3 = _f2.subs(sin('B'), 'b/(2*r)').subs(cos('B'), '(a**2 + c**2 - b**2)/(2*a*c)')
        if _f3 != _f2:
            af.add_step(['\\because ' + BaseEq([_f2, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f3, '0']).printing()])
            _eqs.append(['((a**2 + c**2 - b**2)/(2*a*c))**2 + (b/(2*r))**2', '1'])
        _f4 = _f3.subs(sin('C'), 'c/(2*r)').subs(cos('C'), '(a**2 + b**2 - c**2)/(2*a*b)')
        if _f4 != _f3:
            af.add_step(['\\because ' + BaseEq([_f3, '0']).printing(), '由正弦余弦定理得:' + BaseEq([_f4, '0']).printing()])
            _eqs.append(['((a**2 + b**2 - c**2)/(2*a*b))**2 + (c/(2*r))**2', '1'])
        _eqs.append([_f4, '0'])
    if flag:  # 如果可以，那么设其中一条边的值为1
        assuem_eq = ['b', '1']
        # _m = sympify('m')
        # af.add_step(['假设令' + BaseEq(['b', 'm']).printing(), '其中 ' + BaseIneq([_m, '>', '0']).printing()])
        extend_eqs = list(chain(_eqs, [assuem_eq]))
        # _extend_eqs = list(chain(_eqs, [['b', 'm']]))
        # af.add_step(['得到方程组', BaseEqs(_extend_eqs).printing()])
        af.add_step(['得到方程组', BaseEqs(_eqs).printing()])
        abc_list = co.isolve_eqs(BaseEqs(extend_eqs), [_a, _b, _c])
        # 合法的边
        abc_list = filter(
            lambda _: sjx_u.legal_sided(_[0][1]) and sjx_u.legal_sided(_[1][1]) and sjx_u.legal_sided(
                _[2][1]), abc_list)
        # 是否满足两边之和大于第三边
        abc_list = list(filter(lambda _: sjx_u.legal_sides((_[0][1], _[1][1], _[2][1])), abc_list))
        a_v, b_v, c_v = map(simplify, zip(*abc_list[0])[1])
        _abc_print_eqs = [[_a, a_v * _b], [_c, c_v * _b]]
        af.add_step(['验证求得:', BaseIneqs(_abc_print_eqs).printing()])
        cos_a = simplify((b_v ** 2 + c_v ** 2 - a_v ** 2) / (2 * b_v * c_v))
        action_results[cos('A')] = cos_a
        sin_a = sqrt(1 - cos_a ** 2)
        action_results[sin('A')] = sin_a
        tan_a = sin_a / cos_a
        if not isinstance(tan_a, ComplexInfinity):
            action_results[tan('A')] = tan_a
        cot_a = cos_a / sin_a
        if not isinstance(cot_a, ComplexInfinity):
            action_results[cot('A')] = cot_a
        cos_b = simplify((a_v ** 2 + c_v ** 2 - b_v ** 2) / (2 * a_v * c_v))
        action_results[cos('B')] = cos_b
        sin_b = sqrt(1 - cos_b ** 2)
        action_results[sin('B')] = sin_b
        tan_b = sin_b / cos_b
        if not isinstance(tan_b, ComplexInfinity):
            action_results[tan('B')] = tan_b
        cot_b = cos_b / sin_b
        if not isinstance(cot_b, ComplexInfinity):
            action_results[cot('B')] = cot_b
        cos_c = simplify((a_v ** 2 + b_v ** 2 - c_v ** 2) / (2 * a_v * b_v))
        action_results[cos('C')] = cos_c
        sin_c = sqrt(1 - cos_c ** 2)
        action_results[sin('C')] = sin_c
        tan_c = sin_c / cos_c
        cot_c = cos_c / sin_c
        if not isinstance(tan_c, ComplexInfinity):
            action_results[tan('C')] = tan_c
        if not isinstance(cot_c, ComplexInfinity):
            action_results[cot('C')] = cot_c
        l1 = permutations([_a, _b, _c], 2)
        rs = map(lambda _: (_[0] / _[1], (_[0] / _[1]).subs(((_a, a_v), (_b, b_v), (_c, c_v)))), l1)
        action_results.update(dict(rs))
        _eqs = action_results.items()
        af.add_step(['求得', BaseEqs(_eqs).printing()])
        return action_results, af
    else:
        raise Exception('Type Match Error')


# 有两个条件的作用函数
two_cond_action_funcs = [_two_cond_action_1, _two_cond_action_2, _two_cond_action_3, _two_cond_action_4,
                         _two_cond_action_5]

if __name__ == '__main__':
    pass
